#pragma once

#include <QWidget>
#include <QTimer>
#include "imageloader.h"
#include "piocore.h"

class ImageView : public QWidget
{
    Q_OBJECT
public:
    explicit ImageView(QWidget *parent = 0);
    virtual ~ImageView();
    void setCore(PioCore * core);

signals:
    void loaded(QImage image);
    void closeImage();
    void prev();
    void next();

public slots:
    void onUpdate(MediaItemPtr item);
    void onRemove(MediaItemPtr item);
    void load(MediaItemPtr item);
    void frame(const QImage & image);
    void clear();
    void addOverlay(QWidget * widget);
    void removeOverlay(QWidget * widget);

protected:
    virtual void paintEvent(QPaintEvent *);
    virtual void resizeEvent(QResizeEvent *);
    virtual void mouseDoubleClickEvent(QMouseEvent *);
    virtual void wheelEvent(QWheelEvent *);
    virtual void keyPressEvent(QKeyEvent *);

private:
    QStringList exifinfo(QString filename);
    void run();

    enum InfoType {
        NoInfo,
        InfoTitle,
        InfoDate,
        InfoExif,
        InfoMax
    };

    class LoaderThread : public QThread {
    public:
        LoaderThread(ImageView * imageview) : m_imageview(imageview) {}
    protected:
        virtual void run() { m_imageview->run(); }
    private:
        ImageView * m_imageview;
    };

    MediaItemPtr m_item;
    QImage m_image;
    QColor m_color1, m_color2, m_color3;
    QFont m_font1, m_font2;
    QString m_title;
    QString m_info1, m_info2;
    int m_infotype;
    PioCore * m_core;
    QList<QWidget*> m_overlays;
    QPixmap m_check;

    QMutex m_mutex;
    QWaitCondition m_cond;
    LoaderThread m_thread;
    bool m_stop;
    QString m_filename;
    QSize m_size;
};
